package com.qianfeng.openapi.web.master.controller;

import com.github.pagehelper.PageInfo;
import com.qianfeng.openapi.web.master.bean.AjaxResponse;
import com.qianfeng.openapi.web.master.bean.TableData;
import com.qianfeng.openapi.web.master.pojo.Customer;
import com.qianfeng.openapi.web.master.service.CustomerService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * @Author LJF
 * @Date 2020/6/29 19:28
 * @Version 1.0
 */
@RestController
@RequestMapping("/sys/customer")
@CrossOrigin(allowCredentials = "true")
public class CustomerController {

    @Autowired
    private CustomerService customerService;


    /**
     * 展示所有的客户
     * @param page  页数
     * @param limit 每页默认数量
     * @return  分页参数
     */
    @GetMapping("/table")
    public TableData<Customer> table(@RequestParam(defaultValue = "1") Integer page,
                                     @RequestParam(defaultValue = "5") Integer limit) {

        //获取分页数据
        PageInfo<Customer> customerByPage = customerService.getCustomerByPage(page, limit);
        //创建TableData对象
        TableData<Customer> tableData = new TableData<>();

        //设置tableData对象的数量
        tableData.setCount(customerByPage.getTotal());
        //设置tableData的集合对象
        tableData.setData(customerByPage.getList());

        return tableData;
    }

    /** 添加方法 */
    @PostMapping("/add")
    public AjaxResponse add(Customer customer){
        try {
            customerService.insertCustomer(customer);
            return new AjaxResponse(true,"添加成功");
        }catch (Exception e){
            e.printStackTrace();
            return new AjaxResponse(false,"添加失败");
        }
    }

    /**删除方法 */
    @RequestMapping("/delete")
    public AjaxResponse delete(Long[] ids){
        try {
            customerService.deleteCustomers(ids);
            return new AjaxResponse(true,"删除成功");
        }catch (Exception e){
            e.printStackTrace();
            return new AjaxResponse(false,"删除失败");
        }

    }

    /** 更新操作 */
    @RequestMapping("/update")
    public AjaxResponse update(Customer customer){
        try {
            customerService.updateCustomer(customer);
            return new AjaxResponse(true,"更新成功");
        }catch (Exception e){
            e.printStackTrace();
            return new AjaxResponse(false,"更新失败");
        }
    }

    @RequestMapping("/search")
    public TableData<Customer> search(@RequestParam(defaultValue = "1") Integer page,
                                      @RequestParam(defaultValue = "5") Integer limit,
                                      String username,Integer state){

        PageInfo<Customer> pageInfo = customerService.selectCustomerByCondition(page,limit,username,state);
        TableData<Customer> tableData = new TableData<>();
        tableData.setCount(pageInfo.getTotal());
        tableData.setData(pageInfo.getList());
        return tableData;
    }

    @RequestMapping("/tree")
    public List<Customer> tree(){
        return customerService.getAllCustomers();
    }
}
